package com.ctv.my.customview.MaiCustomView.WaveSphericalView;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.RelativeLayout;

import com.ctv.my.customview.R;


public class WaveSphericalActivity extends AppCompatActivity {

    private WaveSphericalView waveSphericalView;
    private float maxPercent = 95f;
    private float percent = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wave_spherical);
        waveSphericalView = (WaveSphericalView) findViewById(R.id.waveView);
        RelativeLayout.LayoutParams para;
        para = (RelativeLayout.LayoutParams) waveSphericalView.getLayoutParams();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        float width = dm.widthPixels * 0.3f;
        para.height = (int) width;
        para.width = (int) width;
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (handler.hasMessages(DRAW_VIEW)) {
                    handler.removeMessages(DRAW_VIEW);
                }
                waveSphericalView.clear();
                percent = 0;
                handler.sendEmptyMessageDelayed(DRAW_VIEW, 20);
            }
        });
    }

    private static final int DRAW_VIEW = 1;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case DRAW_VIEW:
                    if (percent <= (maxPercent / 100.0)) {
                        waveSphericalView.setScore(maxPercent);
                        waveSphericalView.setPercent(percent);
                        percent += 0.01f;
                        handler.sendEmptyMessageDelayed(DRAW_VIEW, 20);
                    }
                    break;
            }
        }
    };
}
